package com.xbts.system.service.impl;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.vpc.v3.VpcClient;
import com.huaweicloud.sdk.vpc.v3.model.BatchCreateSecurityGroupRulesRequest;
import com.huaweicloud.sdk.vpc.v3.model.BatchCreateSecurityGroupRulesResponse;
import com.huaweicloud.sdk.vpc.v3.region.VpcRegion;
import com.huaweicloud.sdk.vpc.v3.*;
import com.xbts.common.core.redis.RedisCache;
import com.xbts.common.enums.SettingEnum;
import com.xbts.common.exception.ServiceException;
import com.xbts.common.utils.StringUtils;
import com.xbts.system.domain.dto.AccountSetting;
import com.xbts.system.domain.dto.RegionIdDTO;
import com.xbts.system.service.ICloudServiceV3Service;
import com.xbts.system.service.ICnRegionService;
import com.xbts.system.service.SettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class CloudServiceV3ServiceImpl implements ICloudServiceV3Service {

    @Autowired
    private SettingService settingService;
    @Autowired
    private ICnRegionService cnRegionService;
    @Autowired
    private RedisCache redisCache;


    @Override
    public BatchCreateSecurityGroupRulesResponse batchCreateSecurityGroupRules(RegionIdDTO<BatchCreateSecurityGroupRulesRequest> regionIdDTO) {

        VpcClient client = getVpcClient(regionIdDTO.getRegionId());
        BatchCreateSecurityGroupRulesRequest request = regionIdDTO.getBody();
        BatchCreateSecurityGroupRulesResponse response = null;
        try {
            response = client.batchCreateSecurityGroupRules(request);
        } catch (ConnectionException | RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            log.error("批量创建安全组规则失败，错误码：" + e.getErrorCode() + "，错误信息：" + e.getErrorMsg());
            if("VPC.9902".equals(e.getErrorCode())){
                throw new ServiceException("安全组规则重复！");
            }
            throw new ServiceException(e.getErrorMsg());
        }
        return response;
    }
    /**
     * 获取VpcClient
     * @param regionId 区域id
     * @return VpcClient
     */
    public VpcClient getVpcClient(String regionId){
        AccountSetting accountSetting = settingService.getAccountSetting(SettingEnum.HUAWEI);

        regionId = StringUtils.isEmpty(regionId) ? accountSetting.getRegionId() : regionId;
        ICredential auth = new BasicCredentials()
                .withAk(accountSetting.getAccessKeyId())
                .withSk(accountSetting.getSecretAccessKey());

        return VpcClient.newBuilder()
                .withCredential(auth)
                .withRegion(VpcRegion.valueOf(regionId))
                .build();
    }

}
